import { _decorator, Component, find, instantiate, Label, math, Node, Pool, Prefab, Sprite, SpriteFrame, SpriteFrameEvent, v3 } from 'cc';
import { UIBase } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIBase';
import { ResMgr } from '../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { PropLayer } from '../Game/ModuleLayer/PropLayer';
import { ModuleBase } from '../../FrameWork3.8.6/FrameWork/Module/ModuleBase';
import { ModuleMgr } from '../../FrameWork3.8.6/FrameWork/Module/ModuleMgr';
import { PropertyDt, ShiBingDt } from '../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { DataMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { DataBaseMgr, EquipmentDtMgr, PlayerSkillDtMgr, PropertyDtMgr, ShiBingDtMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { BagManager } from '../Game/Manager/BagManager';
import { FollowCamera } from '../Game/Object/FollowCamera';
import { Emitter } from '../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { GameManager } from '../Game/Manager/GameManager';
const { ccclass, property } = _decorator;

@ccclass('GameOverPanel')
export class GameOverPanel extends UIBase {
    private gameOver:string="游戏失败";

    private lootNodePool:Pool<Node>=null; 

    //道具层
    private _propLayer:PropLayer=null;

    //当前道具随机出来的装备,技能或者兵种
    private _arrPropertyDt:PropertyDt[]=[];

    //已经生成出来的节点数据
    private _mapLoot:Map<string,Node>=new Map();

    //通过道具拿到的金币数量
    private _coinNum:number=0;

    onInit(...rest: any[]): void {
        this._propLayer=ModuleMgr.instance.getModule<PropLayer>("PropLayer");
        if(rest[0]){
            this.gameOver="游戏胜利";
        }
        this.initData();

        //注册返回选关按钮
        this.addClickEventListener("_Backbtn",()=>{
            //获取Camera脚本
            let camera=find("Canvas/FollowCamera").getComponent(FollowCamera);
            //这里通过发送消息给相机
            Emitter.instance.emit("RemoveCamera",camera);
            GameManager.instance.coinNum+=this._coinNum;
            this.closeUI("MainUI",true,true);
            this.closeUI("GameUI",true,true);
            this.openUI("LevelPanel",true,true);
            this.openUI("Menu");
        })
    }

    //初始化界面信息
    initData(){
        //拿到需要关注的节点与脚本
        let gameOverLabel=this.getComp<Label>("_GameOverPanel","Label");
        let coinLabel=this.getComp<Label>("_CoinLabel","Label");

        let lootprefab=ResMgr.instance.getRes<Prefab>(BundleConfig.Game.name,"Loot");
        this.lootNodePool=new Pool<Node>(()=>{
            let node=instantiate(lootprefab);
            return node;
        },3,()=>{
            this.node.destroy();
        })

        //初始化当前信息
        gameOverLabel.string=this.gameOver;

        //添加所有金币
        let arrPropDt=this._propLayer.arrGetPropDt;
        if(arrPropDt.length<=0){
            console.log("没捡到装备");
            coinLabel.string="0";
            return;
        }
        for(let propDt of arrPropDt){
            if(propDt.type=="金币"){
                this._coinNum+=propDt.buffID[0];
                coinLabel.string=this._coinNum.toString();
                continue;
            }
            //代码运行到这里就是说明道具是宝箱
            let random=Math.floor(math.random()*propDt.buffID.length)
            let id=propDt.buffID[random];
            //通过id区分是哪个数据管理者的数据
            let idFirst=id.toString().charAt(0);
            switch(idFirst){
                case "2":
                    this._arrPropertyDt.push(DataMgr.instance.getMgr<ShiBingDtMgr>("ShiBingDtMgr").getDataByID<ShiBingDt>(id));
                    break;
                case "3":
                    this._arrPropertyDt.push(DataMgr.instance.getMgr<PlayerSkillDtMgr>("PlayerSkillDtMgr").getDataByID<ShiBingDt>(id));
                    break;
                case "8":
                    this._arrPropertyDt.push(DataMgr.instance.getMgr<EquipmentDtMgr>("EquipmentDtMgr").getDataByID<ShiBingDt>(id));
                    break;
                default:
                    break;
            }
        }
        this.spawnLoot();




    }

    //通过数据生成战利品并且放到背包系统中
    spawnLoot(){
        //获取到父亲节点
        let getTreasureNode=this.getNode("_GetTreasure");
        for(let i=0;i<this._arrPropertyDt.length;i++){
            if(this.isActive(this._arrPropertyDt[i].kind)){
                return;
            }
            let node=this.lootNodePool.alloc();
            node.getComponent(Sprite).spriteFrame=ResMgr.instance.getRes<SpriteFrame>(BundleConfig.Img.name,this._arrPropertyDt[i].iconName);
            node.setPosition(v3(160*i+node.position.x,node.position.y));
            node.setParent(getTreasureNode);
            this._mapLoot.set(this._arrPropertyDt[i].kind,node);
            //存储到背包数据当中
            BagManager.instance.addBagDt(this._arrPropertyDt[i].type,this._arrPropertyDt[i],false);
        }
    }


    //判段是否是已经生成的节点
    isActive(kind:string){
        let node=this._mapLoot.get(kind);
        if(node){
            let num=parseInt(node.getChildByName("_NumLabel").getComponent(Label).string);
            num++;
            node.getChildByName("_NumLabel").getComponent(Label).string=num.toString();
            return true;
        }

        return false;
    }
}


